<template>
	<view class="community">
		<view class="head_bg" style="height: 60rpx;"></view>
		<view class="big_box">
			<view class="count_tab">
				<view :class="{count_tab_inner: istrue == true,is_active: tabIndex == 1}" @tap="tabSelect" :data-id="1" style="margin-left: 145rpx;">到期设备</view>
				<view :class="{count_tab_inner: istrue == true,is_active: tabIndex == 2}" @tap="tabSelect" :data-id="2" style="margin-right: 145rpx;">缴费记录</view>
			</view>
			<view  v-if="tabIndex == 1">
				<view class="operating_box" style="margin-top: 45rpx;padding-bottom: 0;">
					<view class="equ_list">
						<view class="equ_list_title">
							<view class="equ_list_title_left">到期设备</view>
							<view class="equ_status" style="color: red;">总到期设备:{{dqtotal}}台</view>
						</view>
						<view class="equ_list_comm" v-if="list.length>0" v-for="(item,index) in list" :key="index">
							<checkbox-group class="block">
								<!-- <checkbox class="blue" :class="item.flag?'checked':''" style="transform:scale(0.7)" :checked="item.flag"/> -->
								<text class="equ_list_inner_text">{{item.residential_quarters_name}}</text>
							</checkbox-group>
							<view class="equ_list_comm_inner solid-bottom margin-bottom-sm" v-for="(info,index) in item.childen" :key="index">
								<checkbox-group class="block" @change="checkboxChange(info)">
								   <checkbox class="blue" :value="info.equipment_number" :class="info.flag?'checked':''" style="transform:scale(0.7)" :checked="info.flag"/>								
									<text class="equ_list_inner_text">设备号：{{info.equipment_number}}</text>
									<text class="equ_list_inner_text" style="margin-left: 30rpx;">到期时间：{{info.end_time}}</text>
									<view class="equ_list_inner_text margin-left-xl" style="color:red;line-height:0.5;">
										年费：{{info.annual_fee}}</view>
								</checkbox-group>
							</view>
						</view>
						<!-- <ly-tree ref="tree"
						    :tree-data="list"
							:props="props"
							check-on-click-node
							default-expand-all 
							node-key="menu_code" 
							show-checkbox 
						/> -->
						<view class="cost_bottom">
							<view class="cost_bottom_left">共{{total}}台 合计： <text class="red_color">{{checkmon}}</text> 元</view>
							<view class="cost_bottom_right" @tap="tocount">结算</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="tabIndex == 2">
				<view class="operating_box" style="margin-top: 45rpx;padding-bottom: 0;">
					<view class="equ_list">
						<view class="cost_pay" v-if="list.length>0" v-for="(item,index) in list" :key="index">
							<view class="cost_pay_inner">
								<text class="equ_list_inner_text">{{item.residential_quarters_name}}</text>
								<text class="equ_list_inner_text red_color">-￥{{item.payment}}元</text>
							</view>
							<view class="cost_pay_inner">
								<text class="equ_list_inner_text">终端编号：{{item.equipment_number}}</text>
								<text class="equ_list_inner_text">{{item.payment_time}}</text>
							</view>
						</view>
					</view>
				</view>
				<view style="height:100rpx;"></view>
				<view v-if="list.length==0" class="margin-top-lg text-center">
					<image src='../../../common/img/no_data.png' style="width:275rpx;height:312rpx;"></image>
				</view>
				<view v-if="list.length>0" class="text-black text-df text-center bg-white padding-sm padding-left-lg be-gray">
					{{loadingTxt}} 共({{list.length}}条记录)
				</view>
			</view>
			</view>
		</view>
	</view>
</template>

<script>
	let page=1,timer=null
	export default {
		data() {
			return {
				loadingTxt:'加载更多',
				tabIndex: 1,
				istrue:true,
				list:[],//到期设备列表
				info:'',
				checkvalue:"",
				checkmon:0,
				total:0,
				dqtotal:0
			}
		},
		onShow() {
			page=1;
			this.getalllist();
		},
		methods: {
			tabSelect(e) {
				page=1;
				this.tabIndex = e.currentTarget.dataset.id;
				this.list=[];
				this.loadingTxt='加载更多',
				this.getalllist();
			},
			getalllist(){
				let url;
				if(this.tabIndex==1){
					url="devicemanagement/v1/expire";
				}else if(this.tabIndex==2){
					url="devicemanagement/v1/payannualfeelog";
				}
				let that=this;
				that.http.tokenreq({
					url: url,
					data:{'pageindex':page}
				}).then((res) => {
					let e=res.data;
					//console.log("e="+JSON.stringify(e.rows));
					if(e.code==0 && e.success && this.tabIndex==1){
						let dqtotal=this.dqtotal;
						let list=e.rows;
						list.forEach(info => {
							info.childen.forEach(item => {
								item.flag = false;
								item.guid = info.residential_quarters_guid;
							});
							dqtotal+=info.devicenum
						});
						console.log("e="+JSON.stringify(list));
						that.list=list;
						this.dqtotal=dqtotal;
						page++;
					}
					if(e.code==0 && e.success && this.tabIndex==2){
						that.list=e.rows;
						page++;
					}
				})
			},
			checkboxChange (item) {
				let checkmon=this.checkmon;
				let total=this.total;
				let value=this.checkvalue;
				item.flag = !item.flag;
				if (item.flag) {
					value+='{residential_quarters_guid:"'+item.guid+'",equipment_number:"'
					+item.equipment_number+'",payment:"'+item.annual_fee+'"},'
					checkmon=Number(checkmon)+Number(item.annual_fee);
					total++;
				}else{
					value=value.replace('{residential_quarters_guid:"'+item.guid+'",equipment_number:"'
					+item.equipment_number+'",payment:"'+item.annual_fee+'"},',"");
					checkmon=Number(checkmon)-Number(item.annual_fee);
					total--;
				}
				console.log("选中="+value)
				this.checkvalue=value;
				this.checkmon=checkmon;
				this.total=total;
			},
			getmorelist(){
				this.loadingTxt='加载中'
				let that=this;
				let url;
				if(this.tabIndex==1){
					url="devicemanagement/v1/expire";
				}else if(this.tabIndex==2){
					url="devicemanagement/v1/payannualfeelog";
				}
				that.http.tokenreq({
					url: url,
					data:{'pageindex':page}
				}).then((res) => {
					uni.hideLoading();
					let e=res.data;
					if(e.code==0 && e.success && that.tabIndex==1){
						if(e.rows.length==0){
							that.loadingTxt="已经加载全部";
							return;
						}
						let dqtotal=this.dqtotal;
						let list=e.rows;
						list.forEach(info => {
							info.childen.forEach(item => {
								item.flag = false;
								item.guid = info.residential_quarters_guid;
							});
							dqtotal+=info.devicenum
						});
						this.dqtotal=dqtotal;
						that.list=that.list.concat(list);
						page++;
					}
					if(e.code==0 && e.success && that.tabIndex==2){
						if(e.rows.length==0){
							that.loadingTxt="已经加载全部";
							return;
						}
						that.list=that.list.concat(e.rows);
						page++;
					}
				})
			},
			onReachBottom() {
				//阻止重复加载
				if(timer !== null){
					clearTimeout(timer)
				}
				timer=setTimeout(()=>this.getmorelist(),500)
			},
			//结算
			tocount(){
				if(this.total==0){
					uni.showToast({
						icon: 'none',
					    title: '未选中任何设备'
					});
					return;
				}
				let that=this;
				let value="["+this.checkvalue+"]"
				that.http.tokenreq({
					url: 'devicemanagement/v1/payannualfee',
					data:{
						"expirelist":value
					}
				}).then((res) => {
					console.log("res="+JSON.stringify(res));
					let e=res.data;
					if(e.code==0 && e.success){
						uni.showModal({
							content: e.message,
							showCancel:false,
							success: function (res) {
								if (res.confirm) {
									page=1;
									this.getalllist();
								}
							}
						});
					}else{
						uni.showToast({
							icon: 'none',
						    title: e.message
						});
					}
				})
			},
		}
	}
</script>
